using System;
using System.Configuration;
using System.Data;
using digit.Core;



namespace SMS.ServiceManager
{
    public class StandingManager
    {
        #region CONSTRUCTORS
        static StandingManager()
        {
            _data = new StandingData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static StandingData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }

        #endregion

        #region METHODS
        public static bool Insert(int cupID, string cupName, string standing, string reporter, string createdBy, DateTime createdDate, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = cupID;
                values[1] = Util.ReplaceNullTo(cupName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(standing, DBNull.Value);
                values[3] = Util.ReplaceNullTo(reporter, DBNull.Value);
                values[4] = Util.ReplaceNullTo(createdBy, DBNull.Value);
                values[5] = Util.ReplaceNullTo(createdDate, DBNull.Value);
                values[6] = Util.ReplaceNullTo(status, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(StandingInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[7];
                values[0] = info.CupID;
                values[1] = Util.ReplaceNullTo(info.CupName, DBNull.Value);
                values[2] = Util.ReplaceNullTo(info.Standing, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.Reporter, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.Status, DBNull.Value);

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(long standingID, int cupID, string cupName, string standing, string reporter, string createdBy, DateTime createdDate, byte status)
        {
            _error = "";
            try
            {
                object[] values = new object[8];
                values[0] = standingID;
                values[1] = cupID;
                values[2] = Util.ReplaceNullTo(cupName, DBNull.Value);
                values[3] = Util.ReplaceNullTo(standing, DBNull.Value);
                values[4] = Util.ReplaceNullTo(reporter, DBNull.Value);
                values[5] = Util.ReplaceNullTo(createdBy, DBNull.Value);
                values[6] = Util.ReplaceNullTo(createdDate, DBNull.Value);
                values[7] = Util.ReplaceNullTo(status, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(StandingInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[8];
                values[0] = info.StandingID;
                values[1] = info.CupID;
                values[2] = Util.ReplaceNullTo(info.CupName, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.Standing, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.Reporter, DBNull.Value);
                values[5] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
                values[6] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
                values[7] = Util.ReplaceNullTo(info.Status, DBNull.Value);

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(long standingID)
        {
            _error = "";
            try
            {
                object[] values = { standingID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(StandingInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.StandingID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static StandingInfo Select(long standingID)
        {
            _error = "";
            try
            {
                object[] values = { standingID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                StandingInfo info = new StandingInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool UpdateStatus(int standingId, byte bytStatus)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = standingId;
                values[1] = bytStatus;
                return _data.UpdateStatus(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static DataTable SelectByCup(int intCup)
        {
            _error = "";
            try
            {
                return _data.SelectByCup(intCup);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        #endregion

    }
}